/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import Platform.User;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author HuuTri
 */
public class StorageUserData {
  
    RecordStore store;
    
    public StorageUserData() throws RecordStoreException{
    store = RecordStore.openRecordStore("UserData",true);
    }
    
    public void closeStorageUserData() throws RecordStoreException{
        store.closeRecordStore();        
    }
    
    public void deleteRecordUserStore() throws RecordStoreException{
        RecordStore.deleteRecordStore("UserData");
    }
    
    public void addUserData(User input) throws RecordStoreException{
        byte bytes[] = input.toString().getBytes();
        store.addRecord(bytes, 0, 1);
    }
    
    public User getUserData(int index) throws RecordStoreException{
        byte bytes[] = store.getRecord(index);
        if (bytes.length == 0) return null;
        
        String temp = new String(bytes,0,bytes.length);
        User user = new User();
        user = user.convertFromString(temp);
        
        return user;
    }
}
